﻿<%@ WebHandler Language="C#" Class="HandlerCtr" %>

using System;
using System.Web;
using System.Data.SqlClient;

public class HandlerCtr : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string strConn = LinkToDatabase.strConn;
        int id = int.Parse(context.Request.QueryString["ctrId"]);
        using (SqlConnection conn = new SqlConnection(strConn))
        {
            string strCmd = @" IF EXISTS (SELECT * FROM main_page_ctr
                                            WHERE id=@id) 
                                            UPDATE main_page_ctr SET [count]=[count]+1
                                            WHERE id=@id
                                            ELSE
                                            INSERT INTO main_page_ctr (id,[count]) VALUES (@id,1)";
            using (SqlCommand cmd = new SqlCommand(strCmd, conn))
            {
                cmd.Parameters.AddWithValue("@id", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}